package com.sunday.android.fangdaifu.bukong.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.sunday.android.fangdaifu.bukong.R;
import com.sunday.common.utils.DeviceUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by 刘涛 on 2017/7/20.
 */

public class SelectMapWindow extends PopupWindow {


    @Bind(R.id.btn1)
    TextView btn1;
    @Bind(R.id.btn2)
    TextView btn2;
    @Bind(R.id.btn_cancel)
    TextView btnCancel;
    private Context mContext;
    private String oriLatit, oriLongit, tarLatit, tarLongit;

    public SelectMapWindow(Context context, String oriLatit, String oriLongit, String tarLatit, String tarLongit) {
        mContext = context;
        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_select_direction, null);
        setContentView(view);
        ButterKnife.bind(this, view);
        this.oriLatit = oriLatit;
        this.oriLongit = oriLongit;
        this.tarLatit = tarLatit;
        this.tarLongit = tarLongit;
        int appWidth = DeviceUtils.getDisplay(mContext).widthPixels;
        this.setWidth(appWidth);
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setOutsideTouchable(true);
        this.setFocusable(true);
        this.setOnDismissListener(new PoponDismissListener());
        this.setBackgroundDrawable(new BitmapDrawable());
        //this.setAnimationStyle(R.style.Theme_AppCompat_Dialog_Alert);


    }


    @OnClick({R.id.btn1, R.id.btn2, R.id.btn_cancel})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn2:
                if (isAvilible(mContext, "com.baidu.BaiduMap")) {//传入指定应用包名
                    Intent intent = new Intent();
                    String uri = String.format("baidumap://map/direction?&origin=%1$s,%2$s&destination=%3$s,%4$s&mode=driving", oriLatit, oriLongit, tarLatit, tarLongit);
                    intent.setData(Uri.parse(uri));
                    mContext.startActivity(intent); //启动调用

                } else {//未安装
                    //market为路径，id为包名
                    //显示手机上所有的market商店
                    Toast.makeText(mContext, "您尚未安装百度地图", Toast.LENGTH_LONG).show();
                    Uri uri = Uri.parse("market://details?id=com.baidu.BaiduMap");
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    mContext.startActivity(intent);
                }


                break;
            case R.id.btn1:
                if (isAvilible(mContext, "com.autonavi.minimap")) {
                    Intent intent = new Intent();
                    String uri = String.format("androidamap://route?sourceApplication=房大夫&slat=%1$s&slon=%2$s&dlat=%3$s&dlon=%4$s&dev=0&m=0&t=1",oriLatit,oriLongit,tarLatit,tarLongit);
                    intent.setData(Uri.parse(uri));
                    mContext.startActivity(intent); //启动调用
                } else {
                    Toast.makeText(mContext, "您尚未安装高德地图", Toast.LENGTH_LONG).show();
                    Uri uri = Uri.parse("market://details?id=com.autonavi.minimap");
                   Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    mContext.startActivity(intent);
                }

                break;
            case R.id.btn_cancel:
                dismiss();
        }
    }


    //显示
    public void showPopupWindow(View parent) {
        if (!this.isShowing()) {
            this.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
            backgroundAlpha(0.5f);
        } else {
            this.dismiss();
        }
    }


    /**
     * 弹出的popWin关闭的事件，主要是为了将背景透明度改回来
     */
    class PoponDismissListener implements OnDismissListener {

        @Override
        public void onDismiss() {
            // TODO Auto-generated method stub
            //Log.v("List_noteTypeActivity:", "我是关闭事件");
            backgroundAlpha(1f);
            dismiss();
        }
    }

    /**
     * 设置添加屏幕的背景透明度
     */
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = ((Activity) mContext).getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        ((Activity) mContext).getWindow().setAttributes(lp);
    }

    /**
     * 检查手机上是否安装了指定的软件
     *
     * @param context
     * @param packageName：应用包名
     * @return
     */
    public static boolean isAvilible(Context context, String packageName) {
        //获取packagemanager
        final PackageManager packageManager = context.getPackageManager();
        //获取所有已安装程序的包信息
        List<PackageInfo> packageInfos = packageManager.getInstalledPackages(0);
        //用于存储所有已安装程序的包名
        List<String> packageNames = new ArrayList<String>();
        //从pinfo中将包名字逐一取出，压入pName list中
        if (packageInfos != null) {
            for (int i = 0; i < packageInfos.size(); i++) {
                String packName = packageInfos.get(i).packageName;
                packageNames.add(packName);
            }
        }
        //判断packageNames中是否有目标程序的包名，有TRUE，没有FALSE
        return packageNames.contains(packageName);
    }


}



